import AppRouter from "./router/AppRouter";
import http from "./utils/http/http";
import byteConversion from "./utils/http/byteConversion";
import generateId from "./utils/http/generateId";
import { useEffect } from "react";

import { useGlobalVariablesController, setAria2GetGlobalStat } from "./context/GlobalContext";

function App() {
  const [controller, dispatch] = useGlobalVariablesController();

  useEffect(() => {
    const intervalId = setInterval(() => {
      // 发起POST请求;
      http
        .post("http://115.171.37.56:6800/jsonrpc", {
          jsonrpc: "2.0",
          method: "aria2.getGlobalStat",
          id: generateId(),
        })
        .then((response) => {
          // 处理响应数据
          let apiResult = response.result;
          //console.log(apiResult);
          apiResult.downloadSpeed = byteConversion(apiResult.downloadSpeed);
          apiResult.uploadSpeed = byteConversion(apiResult.uploadSpeed);
          setAria2GetGlobalStat(dispatch, apiResult);
          //console.log(response);
        })
        .catch((error) => {
          // 处理错误
          // 处理
          console.error(error);
        });
    }, 1000);

    return () => clearInterval(intervalId);

    // 发起GET请求
    // http.get('/users', { page: 1 })
    //   .then((response) => {
    //     // 处理响应数据
    //     console.log(response);
    //   })
    //   .catch((error) => {
    //     // 处理错误
    //     console.error(error);
    //   });
  }, []);

  return <AppRouter />;
}

export default App;
